package com.manger.format;

import com.manger.common.office.sax.ExcelFactory;
import com.manger.model.Project;
import com.manger.model.XWPersonAndCar;
import com.manger.utils.CopyFiles;
import com.manger.utils.LoanFileEnum;
import com.manger.utils.SystemConstant;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@ClassName XWPayCaseFilesFormat
 *@Description  TODO
 *@Author @LiuHongJun
 *@Date 2025/9/1 16:01
 *@Version 1.0
 */
public class XWPayCaseFilesFormat implements CaseFilesFormat{

    private static  List<String> companyIgnoreList;

    private static  List<String> personIgnoreList;

    private static  List<String> carIgnoreList;

    private static String companyIgnore= "委托代理服务合同,授权委托书,证据材料清单,营业执照,法定代表人身份证明书,金融许可证,法人身份证正反面,民事起诉状-简版,民事起诉状";

    private static String personIgnore = "委托代理服务合同,授权委托书,证据材料清单,营业执照,法定代表人身份证明书,金融许可证,法人身份证正反面,民事起诉状-简版,民事起诉状";

    private static String carIgnore = "商业保险单&交强险保单,委托代理服务合同,授权委托书,证据材料清单,营业执照,法定代表人身份证明书,金融许可证,法人身份证正反面,民事起诉状-简版,民事起诉状";
    @Override
    public void format(String extractDir, String targetDir, Project project) throws Exception {
        File file = new File(extractDir);
        if(!file.exists()){
            System.out.println("没有找到文件夹!");
            return;
        }
        companyIgnoreList = Arrays.asList(companyIgnore.split(","));
        personIgnoreList = Arrays.asList(personIgnore.split(","));
        carIgnoreList = Arrays.asList(carIgnore.split(","));

        File[] files = file.listFiles();
        for(File e : files){
            String fileName = e.getName();
            if(fileName.contains("企贷")){
                handleCompanyCase(e.getCanonicalPath(),targetDir,project,companyIgnoreList);
            }else if(fileName.contains("个贷") || fileName.contains("车贷")){
                String excelFilePath = extractDir + "/" + fileName + "/" + SystemConstant.ENTRUSTED_INFO;
                File excelFile = new File(excelFilePath);
                System.out.println(excelFile.getCanonicalPath());
                if(!excelFile.exists()){
                    System.out.println("【个贷，车贷】没有找到委托信息！");
                    return;
                }
                Map<String , List<?>> map = ExcelFactory.getExcelFactory().getParseExecutor().parse(excelFile, XWPersonAndCar.class).get();
                if(map == null || map.isEmpty()){
                    return;
                }
                List<XWPersonAndCar> xwPersonAndCarList = (List<XWPersonAndCar>)  map.get(map.keySet().iterator().next());
                if(xwPersonAndCarList == null || xwPersonAndCarList.isEmpty()){
                    System.out.println("【个贷，车贷】没有解析到委托信息！");
                    return;
                }
                handlePersonAndCarCase(e.getCanonicalPath(),targetDir,project,xwPersonAndCarList);
            }

        }
    }

    private void handlePersonAndCarCase(String resourceFilePath,String targetPath,Project project,
                                        List<XWPersonAndCar> xwPersonAndCarList) throws Exception{

        Map<String,String> idCardMap = new HashMap<String,String>();
        File file = new File(resourceFilePath);
        File[] files = file.listFiles();
        for(File e : files){
            if(e.isFile()){
                continue;
            }
            File[] caseFileDirs = e.listFiles();
            for(File caseFileDir : caseFileDirs){
                String fileName = caseFileDir.getName();
                XWPersonAndCar tempXWPersonAndCar = null;
                for(XWPersonAndCar xwPersonAndCar : xwPersonAndCarList){
                    String mark = xwPersonAndCar.getCustNo() + "-" + xwPersonAndCar.getCustName();
                    if(fileName.contains(mark)){
                        tempXWPersonAndCar = xwPersonAndCar;
                        break;
                    }
                }
                if(tempXWPersonAndCar == null){
                    continue;
                }
                System.out.println("整理个贷，车贷：【"+caseFileDir.getName()+"】");
                File[] mFiles = caseFileDir.listFiles();
                for(File mFile : mFiles){
                    boolean hasIgnore = false;
                    if(e.getName().contains("车贷")){
                        for(String str: carIgnoreList){
                            if(mFile.getName().contains(str) && !mFile.getName().contains("民事起诉状-要素式")){
                                hasIgnore = true;
                            }
                        }
                    }else if (e.getName().contains("个贷")){
                        for(String str: personIgnoreList){
                            if(mFile.getName().contains(str) && !mFile.getName().contains("民事起诉状-要素式")){
                                hasIgnore = true;
                            }
                        }
                    }else {
                        continue;
                    }
                    if(hasIgnore){
                        continue;
                    }
                    String dirName = mFile.getName().contains("民事起诉状") ? LoanFileEnum.INDICTMENT.getName() : LoanFileEnum.EVIDENCE_MATERIALS.getName();
                    String tempTargetPath = targetPath + "/" + project.getDebtName() + "/" + "个贷，车贷" + "/"
                            + SystemConstant.CASE_FILE_DIR_NAME + "/" + tempXWPersonAndCar.getCustName() + "-" + tempXWPersonAndCar.getIdCard() + "/" ;
                    File tempFile = new File(tempTargetPath);
                    if(!tempFile.exists()){
                        tempFile.mkdirs();
                    }
                    CopyFiles.copyFileToDir(mFile.getCanonicalPath(),tempTargetPath + "/" + dirName);
                    CopyFiles.copyBaseFiles(tempTargetPath,project);
                    idCardMap.put(tempXWPersonAndCar.getIdCard(),tempTargetPath);
                }
            }
        }
        CopyFiles.copyOtherMaterialFileToDir(resourceFilePath,idCardMap);
    }

    private void handleCompanyCase(String resourceFilePath,String targetPath,Project project,List<String> companyIgnoreList) throws Exception{

        Map<String,String> tempMap = new HashMap<>();
        resourceFilePath += "/" + "企贷";
        File file  = new File(resourceFilePath);
        if(!file.exists()){
            System.out.println("企贷没有找到材料数据！");
            return;
        }
        File[] files = file.listFiles();
        for (File e : files) {
            String[] arr = e.getName().split("-");
            String companyName = arr[1];
            File[] mFiles = e.listFiles();
            System.out.println("整理企业贷：【"+e.getName()+"】");
            for(File mFile : mFiles){
                boolean hasIgnore = false;
                for(String ignore : companyIgnoreList){
                    if(mFile.getName().contains(ignore)
                            && !mFile.getName().contains("民事起诉状-要素式")
                            && !mFile.getName().contains("客户营业执照")
                            && !mFile.getName().contains("法人授权委托书")){
                        hasIgnore = true;
                        break;
                    }
                }
                if(hasIgnore){
                    continue;
                }
                String dirName = mFile.getName().contains("民事起诉状") ? LoanFileEnum.INDICTMENT.getName() : LoanFileEnum.EVIDENCE_MATERIALS.getName();
                String targetDirPath = targetPath + "/" + project.getDebtName() + "/" + "企贷" + "/" + SystemConstant.CASE_FILE_DIR_NAME + "/" + companyName + "/" + dirName;
                File targetFile = new File(targetDirPath);
                if(!targetFile.exists()){
                    targetFile.mkdirs();
                }
                CopyFiles.copyFileToDir(mFile.getCanonicalPath(),targetFile.getCanonicalPath());
                tempMap.put(companyName,targetFile.getParentFile().getCanonicalPath());
            }
        }
        CopyFiles.copyOtherMaterialFileToDir(new File(resourceFilePath).getParentFile().getCanonicalPath(),tempMap);
    }

    public static void main(String[] args) throws Exception{
        XWPayCaseFilesFormat xwPayCaseFilesFormat = new XWPayCaseFilesFormat();
        Project project = new Project();
        project.setDebtName("新网");
        String extractDir = "C:\\Users\\liuhj\\Desktop\\新网";
        String targetDir = "C:\\Users\\liuhj\\Desktop\\案件整理结果";
        xwPayCaseFilesFormat.format(extractDir,targetDir,project);
    }
}
